//
//  UILabel+LBExtend.m
//  LBCategoryLibrary
//
//  Created by smufs on 2017/3/8.
//  Copyright © 2017年 李冰. All rights reserved.
//

#import "UILabel+LBExtend.h"

@implementation UILabel (LBExtend)

- (CGFloat)lb_textWidth {

//    CGFloat width = [self.text sizeWithAttributes:@{NSFontAttributeName:self.font}].width;
    CGFloat width = [self labelSizeWithMaxWidth:CGFLOAT_MAX textFont:self.font].width;
    return width;
}

- (CGFloat)lb_textHeight {

//    CGFloat height = [self.text sizeWithAttributes:@{NSFontAttributeName:self.font}].height;
    CGFloat height = [self labelSizeWithMaxWidth:CGFLOAT_MAX textFont:self.font].height;
    return height;
}

- (CGSize) labelSizeWithMaxWidth:(CGFloat)width textFont:(UIFont *)font{
    
    CGRect rect = [self.text boundingRectWithSize:CGSizeMake(width, MAXFLOAT)
                                          options:\
                   NSStringDrawingTruncatesLastVisibleLine |
                   NSStringDrawingUsesLineFragmentOrigin |
                   NSStringDrawingUsesFontLeading
                                       attributes:@{NSFontAttributeName: font}
                                          context:nil];
    
    return rect.size;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
